package com.cw.reduce.filter;

import java.util.Vector;

public class DiffFilter implements IFilter {

	String[] typedif = { "--++", "!--++", "++--", "!++--", "-++-", "!-++-",
			"+--+", "!+--+" }; // number different type

	public DiffFilter() {
	}

	private boolean intChange2value(int value, String format) {

		if (format.startsWith("!")) {
			return !intChange2value(value, format.substring(1));
		}

		int bw = value / 100;
		int sw = value / 10 % 10;
		int gw = value % 10;

		int a = bw;
		int b = 9 + sw - bw;
		int c = 9 + gw - sw;
		int d = 9 - gw;
		if (a < b && a < c && b > d && c > d && format.equals(typedif[0])) {
			return true;
		}
		if (a > b && a > c && b < d && c < d && format.equals(typedif[2])) {
			return true;
		}
		if (a > b && a < c && b > d && c < d && format.equals(typedif[4])) {
			return true;
		}
		if (a > b && a < c && b < d && c > d && format.equals(typedif[6])) {
			return true;
		}

		return false;
	}

	/**
	 * startFilter
	 * 
	 * @param condition
	 *            Vector
	 * @param target
	 *            Vector
	 * @return Vector
	 * @todo Implement this cp.gui.reduce.filter.IFilter method
	 */
	public Vector startFilter(Vector condition, Vector<Integer> target) {
		Vector result = new Vector();
		String condi = (String) condition.elementAt(0);
		for (int i = 0; i < target.size(); i++) {
			int value = target.elementAt(i);
			if (intChange2value(value, condi)) {
				result.add(value);
			}
		}
		return result;
	}
}
